package com.ciwong.epaper.modules.me.adapter;

import com.ciwong.epaper.modules.me.bean.Clazz;
import com.ciwong.mobilelib.utils.ViewHolder;
import com.ciwong.epaper.R;
import com.ciwong.epaper.bean.SelectObject;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * 班级列表Adapter
 *
 * @author bin
 * @version ciwong v.1.0 2015/8/10 17:33
 * @since ciwong v.1.0
 */
public class ClassListAdapter extends BaseAdapter
{
    /**
     * 上下文对象
     */
    private Context mContext;

    /**
     * 班级信息集
     */
    private List<SelectObject> mSelectObjects;

    /**
     * 默认选择的班级
     */
    private SelectObject mSelectObject;

    /**
     * 构造函数
     *
     * @param context
     *            上下文对象
     * @param selectObjects
     *            班级信息集
     * @param mSelectObject
     *            当前选中的学校信息
     */
    public ClassListAdapter(Context context, List<SelectObject> selectObjects,
            SelectObject mSelectObject)
    {
        this.mContext = context;
        this.mSelectObjects = selectObjects;
        this.mSelectObject = mSelectObject;
    }

    @Override
    public int getCount()
    {
        return mSelectObjects != null ? mSelectObjects.size() : 0;
    }

    @Override
    public Object getItem(int position)
    {
        return mSelectObjects != null && !mSelectObjects.isEmpty()
                ? mSelectObjects.get(position) : null;
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View v;
        if (convertView == null)
        {
            v = View.inflate(mContext, R.layout.item_class, null);
        }
        else
        {
            v = convertView;
        }
        TextView tvClassName = ViewHolder.get(v, R.id.tv_item_class);

        TextView tv_teacher_name = ViewHolder.get(v, R.id.tv_teacher_name);

        SelectObject selectObject = (SelectObject) getItem(position);

        tvClassName.setText(selectObject.getName());

//        tv_teacher_name.setText(((Clazz)selectObject).getRoomUserName());
//        ivSelect.setVisibility(this.mSelectObject != null
//                && this.mSelectObject.equals(selectObject) ? View.VISIBLE
//                        : View.INVISIBLE);
        return v;
    }

    public void clear()
    {
        if (this.mSelectObjects != null || !this.mSelectObjects.isEmpty())
        {
            this.mSelectObjects.clear();
            notifyDataSetChanged();
        }
    }

    public void addAll(List<SelectObject> clazzs)
    {
        if (this.mSelectObjects != null)
        {
            this.mSelectObjects.addAll(clazzs);
            notifyDataSetChanged();
        }
    }

    public SelectObject getSelectObject()
    {
        return mSelectObject;
    }


    public void setSelectObject(SelectObject selectObject)
    {
        this.mSelectObject = selectObject;
        notifyDataSetChanged();
    }

}
